<?php
// Datei- bzw. Ordnerrechte
$rights = 0755;
/*
 * Erstellt neue Serien..
 */
require_once('../Database/Database.php');
require_once('../help_funcs.php');

$isGood = true; //TODO: Erstellte Tabellen (und Ordner) wieder löschen, wenn es Fehler gab..

$series_name   = $_POST['series_name'];
$shortcut      = MakeShortcut($series_name, $_POST['shortcut']);
$vehicle_count = intval($_POST['vehicle_count']);
$max_slots     = intval($_POST['max_slots']);
$max_teamslots = intval($_POST['max_teamslots']);
$league_id     = intval($_POST['select_league']);
$steam         = 0;
if(isset($_POST['steamname']))
{
    $steam = intval($_POST['steamname']);
}
$insert_values = array($series_name,  $shortcut, $league_id , $max_slots       , $max_teamslots            , 1       , $steam , 0);
$columns       = array('Name'      , 'Shortcut', 'League_ID', 'Maximum_drivers', 'Maximum_drivers_per_team', 'Season', 'Steam', 'Mulligan');

if($league_id == '' || $series_name == '' || $shortcut == '' || $vehicle_count == '' || $max_slots == '' || $max_teamslots == '')
{
    $isGood = false;
    echo 'Invalid request.<br />';
}

$db = new Database();
$db->Connect();
// DATENBANKTABELLEN ERSTELLEN...
if($isGood && $db->GenerateTables('Series', $shortcut, $league_id, $steam))
{
    echo 'Database structure for ' . $series_name . ' created.<br />';

    if($db->Insert('Series', $columns, $insert_values))
    {
        echo 'Values inserted..<br />';
    }
    else
    {
        echo 'Values could not be inserted in ' . $series_name . '.<br />';
        $isGood = false;
        $db->Disconnect();
    }
}
else
{
    echo 'Database structure could not be created.<br />';
    $isGood = false;
    $db->Disconnect();
}

if($isGood)
{
    // FAHRZEUGE...
    $vehicle_db = $shortcut . '_vehicles';
    for($i = 1; $i <= $vehicle_count; $i++)
    {
        $name   = $_POST['vehicle' . $i . '_name'];
        $engine = $_POST['vehicle' . $i . '_engine'];
        $power  = $_POST['vehicle' . $i . '_power'];
        $rpm    = $_POST['vehicle' . $i . '_rpm'];

        $tempValues  = array($name , $engine , $power        , $rpm);
        $tempColumns = array('Name', 'Engine', 'Engine_Power', 'RPM');

        if($db->Insert($vehicle_db, $tempColumns, $tempValues))
        {
            echo $name . ' added.<br />';
        }
        else
        {
            echo '<b>Could not add ' . $name . '.</b><br />';
        }
    }

    // Hauptkonfiguration..
    if($isGood)
    {
        // ID holen
        $series = $db->SelectColumns('Series', 'ID_Series', 'Shortcut = "' . $shortcut . '"');
        if($series)
        {
            $series_id = $series[0]->ID_Series;
        }
        else
        {
            echo 'Series is not in the database.<br />';
        }
        $db->Disconnect();

        $back_url   = '../index.php?page=all_series';
        $series_url = '../Output/index.php?page=drivers&id=' . $series_id;
        $signup_url = '../Sign-Up/index.php?id=' . $series_id;

        echo '<h2>Wichtige Links</h2>';
        echo '<a href="' . $series_url . '">Frontend</a><br />';
        echo '<a href="' . $signup_url . '">Anmeldung</a><br />';
    }
    else
    {
        echo '<b>Database creation returned errors.<br />';
        echo 'Cannot proceed...</b><br />';
    }
}

if(!$isGood)
{
    if(!$db->DeleteTables($shortcut))
    {
        echo 'Could not delete tables.<br />Please delete them manually.';
    }
}
echo '<a href="' . $back_url . '">Mainmenu</a>';
?>